package com.godofball.medtrack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godofball.medtrack.common.CurrentUser;
import com.godofball.medtrack.dto.PurchaseDetailDto;
import com.godofball.medtrack.exception.PurchaseDetailException;
import com.godofball.medtrack.mapper.PurchaseDetailMapper;
import com.godofball.medtrack.pojo.Drug;
import com.godofball.medtrack.pojo.PurchaseDetail;
import com.godofball.medtrack.service.DrugService;
import com.godofball.medtrack.service.PurchaseDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetail>implements PurchaseDetailService {

    @Autowired
    private DrugService drugService;

    @Override
    public void savePurchaseDetail(PurchaseDetail purchaseDetail) {
        //查看药品是否存在
        Drug drug = drugService.getById(purchaseDetail.getDrugId());
        if(drug==null) throw new PurchaseDetailException("药品不存在");

        //判断生产日期和有效日期是否合理
        if (purchaseDetail.getProduceDate().after(purchaseDetail.getEffectiveDate())){
            throw new PurchaseDetailException("生产日期不能大于有日期");
        }
        //设置管理员
        purchaseDetail.setAdminId(CurrentUser.getCurrentUser());
        //计算总价格
        BigDecimal totalPrice=purchaseDetail.getPurchasePrice().multiply(new BigDecimal(purchaseDetail.getPurchaseCount()));
        purchaseDetail.setTotalPrice(totalPrice);

        this.save(purchaseDetail);
    }

    @Override
    public void updatePurchaseDetail(PurchaseDetail purchaseDetail) {
        if (purchaseDetail.getId()==null) throw new PurchaseDetailException("进货药品项不存在");
        //查看药品是否存在
        Drug drug = drugService.getById(purchaseDetail.getDrugId());
        if(drug==null) throw new PurchaseDetailException("药品不存在");

        //判断生产日期和有效日期是否合理
        if (purchaseDetail.getProduceDate().after(purchaseDetail.getEffectiveDate())){
            throw new PurchaseDetailException("生产日期不能大于有日期");
        }
        //设置管理员
        purchaseDetail.setAdminId(CurrentUser.getCurrentUser());
        //计算总价格
        BigDecimal totalPrice=purchaseDetail.getPurchasePrice().multiply(new BigDecimal(purchaseDetail.getPurchaseCount()));
        purchaseDetail.setTotalPrice(totalPrice);

        this.updateById(purchaseDetail);
    }

    /**
     * 由于要向浏览器响应PurchaseDetailDto对象，需要将PurchaseDetail封装成PurchaseDetailDto
     * @param purchaseDetailPage
     * @return
     */
    @Override
    public Page<PurchaseDetailDto> page(Page<PurchaseDetail> purchaseDetailPage) {

        //根据当前管理员查找purchase_detail的临时数据

        LambdaQueryWrapper<PurchaseDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseDetail::getAdminId,CurrentUser.getCurrentUser());

        //查找进货单号为空的数据，即还没有提交进货单的数据
        queryWrapper.isNull(PurchaseDetail::getPurchaseId);
        this.page(purchaseDetailPage,queryWrapper);

        Page<PurchaseDetailDto> purchaseDetailDtoPage = new Page<>();
        //将purchaseDetailPage中除records的属性复制到purchaseDetailDtoPage
        BeanUtils.copyProperties(purchaseDetailPage,purchaseDetailDtoPage,"records");

        //用于保存purchaseDetailDto并作为purchaseDetailDtoPage的records的值
        LinkedList<PurchaseDetailDto> purchaseDetailDtos = new LinkedList<>();
        //遍历。根据每条数据的drugId查找药品信息，并保存到purchaseDetailDtoPage的records中
        purchaseDetailPage.getRecords().forEach(purchaseDetail -> {
            PurchaseDetailDto purchaseDetailDto = new PurchaseDetailDto();
            //查出药品数据
            Drug drug = drugService.getById(purchaseDetail.getDrugId());
            //将药品信息复制到purchaseDetailDto中
            BeanUtils.copyProperties(drug,purchaseDetailDto);
            //将purchaseDetail中的数据复制到purchaseDetailDto中，
            //drug和purchaseDetail有同名属性--》id,createTime,updateTime，主要复制purchaseDetail的值
            BeanUtils.copyProperties(purchaseDetail,purchaseDetailDto);

            purchaseDetailDtos.add(purchaseDetailDto);
            log.info("purchaseDetailDto:{}",purchaseDetailDto.toString());
        });

        purchaseDetailDtoPage.setRecords(purchaseDetailDtos);

        return purchaseDetailDtoPage;
    }

    @Override
    public void removePurchaseDetailById(Long id) {
        PurchaseDetail purchaseDetail = this.getById(id);

        if (!purchaseDetail.getAdminId().equals(CurrentUser.getCurrentUser())){
            throw new PurchaseDetailException("用户错误");
        }
        this.removeById(id);
    }

    @Override
    public List<PurchaseDetailDto> getListByPurchaseId(Long purchaseId) {
        LambdaQueryWrapper<PurchaseDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseDetail::getPurchaseId,purchaseId);
        List<PurchaseDetail> purchaseDetails = this.list(queryWrapper);

        //用于保存purchaseDetailDto并作为purchaseDetailDtoPage的records的值
        LinkedList<PurchaseDetailDto> purchaseDetailDtos = new LinkedList<>();
        //遍历。根据每条数据的drugId查找药品信息，并保存到purchaseDetailDtoPage的records中
        purchaseDetails.forEach(purchaseDetail -> {
            PurchaseDetailDto purchaseDetailDto = new PurchaseDetailDto();
            //查出药品数据
            Drug drug = drugService.getById(purchaseDetail.getDrugId());
            //将药品信息复制到purchaseDetailDto中
            BeanUtils.copyProperties(drug,purchaseDetailDto);
            //将purchaseDetail中的数据复制到purchaseDetailDto中，
            //drug和purchaseDetail有同名属性--》id,createTime,updateTime，主要复制purchaseDetail的值
            BeanUtils.copyProperties(purchaseDetail,purchaseDetailDto);

            purchaseDetailDtos.add(purchaseDetailDto);
            log.info("purchaseDetailDto:{}",purchaseDetailDto.toString());
        });

        return purchaseDetailDtos;
    }
}
